package com.zxy.ziems.server.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.web.util.TokenUtils;
import com.zxy.ziems.server.domain.Role;
import com.zxy.ziems.server.domain.RoleAuthRel;
import com.zxy.ziems.server.dto.ResourceTree;
import com.zxy.ziems.server.dto.RoleAuthRelDto;
import com.zxy.ziems.server.dto.RoleDto;
import com.zxy.ziems.server.dto.RoleQueryDto;
import com.zxy.ziems.server.service.RoleAuthRelService;
import com.zxy.ziems.server.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户权限管理
 * @author missrain
 * @date 2021/8/30 4:40 下午
 * @Email:wangsiyu-hq@boe.com.cn
 */
@RestController
@RequestMapping("/api/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private RoleAuthRelService roleAuthRelService;

    /**
     * 创建修改角色
     * @param roleDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse addOrUpdate(@RequestBody RoleDto roleDto) {
        return JsonResponse.SUCCESS(roleService.addOrUpdate(roleDto));
    }


    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(@RequestParam(value = "roleId") String roleId) {
        roleService.delete(roleId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 角色分页列表
     * @param dto
     * @return
     */
    @PostMapping("/page/list")
    public JsonResponse<Role> pageList(@RequestBody RoleQueryDto dto) {
        return JsonResponse.SUCCESS(roleService.pageList(dto));
    }

    /**
     * 查询所有角色
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public JsonResponse<Role> list(@RequestBody RoleQueryDto dto) {
        return JsonResponse.SUCCESS(roleService.list(dto));
    }

    /**
     * 查看角色详情
     * @param roleId
     * @return
     */
    @GetMapping("/detail")
    public JsonResponse<RoleDto> detail(@RequestParam String roleId) {
        return JsonResponse.SUCCESS(roleService.detail(roleId));
    }

    /**
     * 根据登录人id查询当前登录人的菜单权限
     * @return
     */
    @GetMapping("/getLoginMenu")
    public JsonResponse<ResourceTree> getLoginMenu() {
        //获取登录人id
        String userId = TokenUtils.getUserIdByToken();
        List<ResourceTree> list = roleService.getLoginMenu(userId);
        //添加根节点
        ResourceTree resourceTree = new ResourceTree();
        resourceTree.setResourceId("root");
        resourceTree.setUrl("root");
        resourceTree.setChildren(list);
        if (CollectionUtil.isNotEmpty(list)) {
            list.stream().forEach(resourceTreeInList->resourceTreeInList.setParentResourceId("root"));
        }
        return JsonResponse.SUCCESS(resourceTree);
    }

    /**
     * 绑定角色权限
     * @param roleAuthRelDto
     * @return
     */
    @PostMapping("/bindDataAuth")
    public JsonResponse bindDataAuth (@RequestBody RoleAuthRelDto roleAuthRelDto) {
        roleAuthRelService.bindDataAuth(roleAuthRelDto);
        return JsonResponse.SUCCESS();
    }

    /**
     * 获取角色权限
     * @param roleId
     * @return
     */
    @GetMapping("/getRoleAuth")
    public JsonResponse<List<RoleAuthRel>> getRoleAuth (@RequestParam(value = "roleId") String roleId) {
        List<RoleAuthRel> roleAuthRelList = roleAuthRelService.getRoleAuth(roleId);
        return JsonResponse.SUCCESS(roleAuthRelList);
    }

    /**
     * 获取全部的资源树
     * @return
     */
    @GetMapping("/getAllResourceTree")
    public JsonResponse<List<ResourceTree>> getAllResourceTree () {
        List<ResourceTree> list = roleService.getAllResourceTree();
        return JsonResponse.SUCCESS(list);
    }

}
